import storeMany from '@/utils/StoreMany'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus';
import { useRouter } from 'vue-router';
const router = useRouter();

let loadingInstance: any = null;
let requestCount = 0;

const loading = () => {
    requestCount++;
    if (!loadingInstance) {
        loadingInstance = ElLoading.service({
            lock: true,
            text: 'Loading...',
            background: 'rgba(0, 0, 0, 0.7)',
        });
    }
}

const hideLoading = () => {
    requestCount--;
    if (requestCount <= 0) {
        loadingInstance.close();
        loadingInstance = null;
    }
}

const notice = (config: any) => {
    ElMessage(config);
}

const isNull = (data: any) => {
    return data === undefined || data === null || data === '';
}
const notNull = (data: any) => {
    return data !== undefined && data !== null && data !== '';
}

const confirm = (config: any, callback: any) => {
    let box = ElMessageBox.confirm(config.message, config.title, config);
    if (callback) {
        box.then(callback);
    }
}

const go = (path: string, param?: any) => {
    return router.push({
        path: path,
        query: param
    });
}

const many = {
    loading,
    hideLoading,
    notice,
    isNull,
    notNull,
    confirm,
    go
};

const Many = many;

// 把 Many 挂载到全局
storeMany.set(Many);

export default Many;